package com.zxs.cart.controller;

import com.zxs.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import parm.CartListParm;
import parm.CartParam;
import pojo.Cart;
import utils.R;

/**
 * @author zxs
 * *8 19:53
 */
@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("save")
    public R save(@RequestBody CartParam cartParam) {

        return cartService.save(cartParam);
    }

    @PostMapping("list")
    public R list(@RequestBody @Validated CartListParm cartListParm, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("购物车查询失败！");
        }
        return cartService.list(cartListParm.getUserId());
    }

    @PostMapping("update")
    public R update(@RequestBody Cart cart){
        return cartService.update(cart);
    }
    @PostMapping("remove")
    public R remove(@RequestBody Cart cart){
        return cartService.remove(cart);
    }


    @PostMapping("admin/check")
    R checkProduct(Integer productId){
        return cartService.checkProduct(productId);
    }

}
